package nxu.it.controller;

import io.jooby.Context;
import io.jooby.MediaType;
import io.jooby.ModelAndView;
import io.jooby.annotation.*;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import nxu.it.model.ApiResult;
import nxu.it.model.Hobby;
import nxu.it.model.Hometown;
import nxu.it.model.RegisterRequest;
import nxu.it.service.GreetingService;
import nxu.it.service.LoginService;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.*;

@Path("/")
@Singleton
public class MainController {
    GreetingService greetingService;
    LoginService loginService = new LoginService();

    @Inject
    public MainController(GreetingService greetingService) {
        this.greetingService = greetingService;
    }

    @GET(value = "/", produces = MediaType.HTML)
    public String index() {
        return "<h1>Yes, it works!</h1>";
    }

    @GET(value = "/bye", produces = MediaType.HTML)
    public String bye(@QueryParam String name) {
        return String.format("<h2>再见,%s</h2>",name);
    }

    @GET(value = "/hello", produces = MediaType.HTML)
    public ModelAndView hello(@QueryParam String username, @QueryParam Integer gender) {
        Map<String, Object> data = new HashMap<>();
        data.put("name", Objects.requireNonNullElse(username, "无名氏"));
        data.put("gender", gender);
        return new ModelAndView("hello.ftl", data);
    }

    @GET(value = "/json", produces = MediaType.JSON)
    public ApiResult json() {
        return ApiResult.ok("请求成功");
    }

    @GET(value = "/render", produces = MediaType.HTML)
    public ModelAndView render(Context ctx) {
        return new ModelAndView("test.ftl", Map.of("name", "Jack"));
    }

    @GET(value = "/login", produces = MediaType.HTML)
    public ModelAndView login() {
        Map<String, Object> data = new HashMap<>();
        return new ModelAndView("login.ftl", data);
    }

    @POST(value = "/loginCheck", produces = MediaType.HTML)
    public String loginCheck(@FormParam String username, @FormParam String password) {
        return loginService.loginCheck(username, password);
    }

    @GET(value = "/register", produces = MediaType.HTML)
    public ModelAndView register() {
        Map<String, Object> data = new HashMap<>();
        List<Hometown> hometownList = new ArrayList<>();
        hometownList.add(new Hometown("ningxia", "宁夏"));
        hometownList.add(new Hometown("shanxi", "陕西"));
        hometownList.add(new Hometown("gansu", "甘肃"));
        hometownList.add(new Hometown("other", "其他"));
        data.put("hometownList", hometownList);
        List<Hobby> hobbyList = new ArrayList<>();
        hobbyList.add(new Hobby("sing", "唱歌"));
        hobbyList.add(new Hobby("dance", "跳舞"));
        hobbyList.add(new Hobby("basketball", "篮球"));
        hobbyList.add(new Hobby("rap", "说唱"));
        data.put("hobbyList", hobbyList);
        return new ModelAndView("register.ftl", data);
    }

    @POST(value = "/doRegister", produces = MediaType.HTML)
    public String doRegister(@FormParam RegisterRequest registerRequest) {
        return registerRequest != null ? registerRequest.toString() : "";
    }

    @GET(value = "/today",produces = MediaType.JSON)
    public Map<String, Object> today() {
        Map<String, Object> data = new LinkedHashMap<>();
        LocalDate date = LocalDate.now();
        data.put("year", date.getYear());
        data.put("month", date.getMonthValue());
        data.put("day", date.getDayOfMonth());


        LocalTime time = LocalTime.now();
        data.put("hour", time.getHour());
        data.put("minute", time.getMinute());

        data.put("weekday", date.getDayOfWeek());
        return data;
    }
}
